package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JButton;

import controller.SupplierController;

public class SupplierTab extends JPanel implements ActionListener{

	JButton findButton, createButton, saveButton, clearButton;
	JLabel searchLabel, findLabel, companyNameLabel, descriptionLabel, addressLabel, postalCodeLabel, cityLabel, phoneLabel, emailLabel, searchException, standardException, cvrNrLabel, discountLabel;
	JTextField searchField, companyNameField, idField, addressField, postalCodeField, cityField, phoneField, emailField, cvrNrField, discountField;
	JPanel row1, row2, row3, row4, row5, row6, row7;
	JTextArea descriptionTextArea;
	SupplierController supplierController = SupplierController.getInstance();
	
	public SupplierTab(){
		
		setLayout(null);
		
		//Row 1
		row1 = new JPanel();
		row1.setBounds(0, 0, 600, 60);
		row1.setLayout(null);
		add(row1);
		searchLabel = new JLabel("Search");
		searchLabel.setBounds(15, 18, 60, 10);
		searchField = new JTextField(15);
		searchField.setBounds(90, 15, 150, 20);
		findButton = new JButton("Find");
		findButton.setBounds(250, 15, 80, 20);
		findButton.addActionListener(this);
		row1.add(searchLabel);
		row1.add(searchField);
		row1.add(findButton);
		//searchException label
		searchException = new JLabel();
		searchException.setBounds(15, 40, 400, 20);
		searchException.setForeground(Color.red);
		row1.add(searchException);
		
		//Row 2
		row2 = new JPanel();
		row2.setBounds(0, 60, 600, 35);
		row2.setLayout(null);
		add(row2);
		cvrNrLabel = new JLabel("CVR/ID");
		cvrNrLabel.setBounds(15, 18, 80, 10);
		cvrNrField = new JTextField(15);
		cvrNrField.setBounds(90, 15, 150, 20);
		row2.add(cvrNrLabel);
		row2.add(cvrNrField);
		//standardException label
		standardException = new JLabel();
		standardException.setBounds(250, 18, 350, 20);
		standardException.setForeground(Color.red);
		row2.add(standardException);
		
		//Row 3
		row3 = new JPanel();
		row3.setBounds(0, 85, 600, 35);
		row3.setLayout(null);
		add(row3);
		companyNameLabel = new JLabel("Company");
		companyNameLabel.setBounds(15, 18, 60, 15);
		companyNameField = new JTextField(15);
		companyNameField.setBounds(90, 15, 150, 20);
		phoneLabel = new JLabel("Phone");
		phoneLabel.setBounds(250, 15, 60, 20);
		phoneField = new JTextField(15);
		phoneField.setBounds(290, 15, 150, 20);
		row3.add(companyNameLabel);
		row3.add(companyNameField);
		row3.add(phoneLabel);
		row3.add(phoneField);
		
		//Row 4
		row4 = new JPanel();
		row4.setBounds(0, 110, 600, 35);
		row4.setLayout(null);
		add(row4);
		addressLabel = new JLabel("Address");
		addressLabel.setBounds(15, 18, 60, 10);
		addressField = new JTextField(15);
		addressField.setBounds(90, 15, 150, 20);
		row4.add(addressLabel);
		row4.add(addressField);
		
		//Row 5
		row5 = new JPanel();
		row5.setBounds(0, 135, 600, 35);
		row5.setLayout(null);
		add(row5);
		postalCodeLabel = new JLabel("Postal Code");
		postalCodeLabel.setBounds(15, 18, 80, 10);
		postalCodeField = new JTextField(15);
		postalCodeField.setBounds(90, 15, 150, 20);
		cityLabel = new JLabel("City");
		cityLabel.setBounds(250, 15, 60, 20);
		cityField = new JTextField(15);
		cityField.setBounds(290, 15, 150, 20);
		row5.add(postalCodeLabel);
		row5.add(postalCodeField);
		row5.add(cityLabel);
		row5.add(cityField);
		
		//Row 6
		row6 = new JPanel();
		row6.setBounds(0, 160, 600, 170);
		row6.setLayout(null);
		add(row6);
		emailLabel = new JLabel("Email");
		emailLabel.setBounds(15, 18, 80, 10);
		emailField = new JTextField(15);
		emailField.setBounds(90, 15, 150, 20);
		row6.add(emailLabel);
		row6.add(emailField);
		discountLabel = new JLabel("Discount");
		discountLabel.setBounds(15, 43, 80, 10);
		discountField = new JTextField(15);
		discountField.setBounds(90, 40, 150, 20);
		row6.add(discountLabel);
		row6.add(discountField);
		descriptionLabel = new JLabel("Description");
		descriptionLabel.setBounds(15, 68, 100, 20);
		descriptionTextArea = new JTextArea();
		descriptionTextArea.setBounds(90, 65, 300, 100);
		descriptionTextArea.setBorder(BorderFactory.createLineBorder(Color.gray));
		row6.add(descriptionLabel);
		row6.add(descriptionTextArea);
		
		//Row 7
		row7 = new JPanel();
		row7.setBounds(0, 325, 600, 80);
		row7.setLayout(null);
		add(row7);
		createButton = new JButton("Create");
		createButton.setBounds(15, 15, 80, 20);
		createButton.addActionListener(this);
		saveButton = new JButton("Save");
		saveButton.setBounds(100, 15, 80, 20);
		saveButton.addActionListener(this);
		clearButton = new JButton("Clear");
		clearButton.setBounds(185, 15, 80, 20);
		clearButton.addActionListener(this);
		row7.add(createButton);
		row7.add(saveButton);
		row7.add(clearButton);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == createButton){
			System.out.println("Creating.");
			try{
				clearException();
				createSupplier();
			} catch(Exception ex) {
				standardException();
			}
		}
		if(e.getSource() == saveButton){
			System.out.println("Saving.");
			try{
				clearException();
				updateSupplier();
			} catch(Exception ex) {
				standardException();
			}
		}
		if(e.getSource() == findButton){
			System.out.println("Searching.");
			try{
				clearException();
				findSupplier();
			} catch(Exception ex) {
				System.out.println(ex);
				searchException();
			}
		}
		if(e.getSource() == clearButton){
			System.out.println("Clearing.");
			clearFields();
		}
	}

	private void createSupplier(){
		supplierController.createSupplier(Integer.parseInt(cvrNrField.getText()), 
				companyNameField.getText(),	descriptionTextArea.getText(), 
				emailField.getText(), Integer.parseInt(phoneField.getText()), 
				addressField.getText(), cityField.getText(),
				Integer.parseInt(postalCodeField.getText()), 
				Double.parseDouble(discountField.getText()));
	}
	
	private void findSupplier(){
		cvrNrField.setText(String.valueOf(supplierController.getCvrNr(Integer.parseInt(searchField.getText()))));
		companyNameField.setText(supplierController.getCompanyName(Integer.parseInt(searchField.getText())));
		descriptionTextArea.setText(supplierController.getDescription(Integer.parseInt(searchField.getText())));
		emailField.setText(supplierController.getEmail(Integer.parseInt(searchField.getText())));
		phoneField.setText(String.valueOf(supplierController.getPhone(Integer.parseInt(searchField.getText()))));
		addressField.setText(supplierController.getAddress(Integer.parseInt(searchField.getText())));
		cityField.setText(supplierController.getCity(Integer.parseInt(searchField.getText())));
		postalCodeField.setText(String.valueOf(supplierController.getPostalCode(Integer.parseInt(searchField.getText()))));
		discountField.setText(String.valueOf(supplierController.getDiscount(Integer.parseInt(searchField.getText()))));
	}
	
	private void updateSupplier(){
		supplierController.updateSupplier(Integer.parseInt(cvrNrField.getText()), companyNameField.getText(), 
				descriptionTextArea.getText(), emailField.getText(),
				Integer.parseInt(phoneField.getText()), addressField.getText(),
				cityField.getText(),Integer.parseInt(postalCodeField.getText()), 
				Double.parseDouble(discountField.getText()));
	}
	
	private void clearFields(){
		searchField.setText(null);
		cvrNrField.setText(null);
		companyNameField.setText(null);
		descriptionTextArea.setText(null);
		emailField.setText(null);
		phoneField.setText(null);
		addressField.setText(null);
		cityField.setText(null);
		postalCodeField.setText(null);
		discountField.setText(null);
		clearException();
	}
	
	private void searchException(){
		searchException.setText("Search Error - Try Again!");
	}
	
	private void standardException(){
		standardException.setText("Error! - Please Check The Information.");
	}
	
	private void clearException(){
		searchException.setText(null);
		standardException.setText(null);
	}
	
}
